# -*- coding: utf-8 -*-
import os
import threading
import requests


class ThreadSafetyList(object):
    def __init__(self):
        self._list = list()
        self._lock = threading.RLock()

    def append(self, item):
        with self._lock:
            self._list.append(item)

    def remove(self, item):
        with self._lock:
            self._list.remove(item)

    def empty(self):
        with self._lock:
            self._list = list()

    def __iter__(self):
        return iter(self._list)

    def __len__(self):
        return len(self._list)

    def __getitem__(self, item):
        return self._list[item]


class ThreadSafetyCounter(object):
    def __init__(self):
        self._num = 0
        self._lock = threading.RLock()

    def add(self, num=1):
        with self._lock:
            self._num += num

    def subtract(self, num=1):
        with self._lock:
            self._num -= num

    def reset(self, num=0):
        with self._lock:
            self._num = num

    def __str__(self):
        return str(self._num)

    def __int__(self):
        return self._num


def create_proxy_session(proxy_url):
    session = requests.session()
    session.proxies = {
        'http': proxy_url,
        'https': proxy_url
    }
    return session


if __name__ == '__main__':
    s = ThreadSafetyList()
    s.append(1)
    s.append(2)
    s.remove(2)
    assert s[0] == 1
